package com.yycx.module.bus.provider.handler.haiying.common;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.enums.DeptTypeEnum;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.client.entity.SysOrganization;
import com.yycx.module.user.client.entity.AppPosition;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自营业务员列表
 *
 * @author zyf
 */
@Component
public class SaleManZyPageListHandler implements PageInterceptor<BusSaleman>, ExportInterceptor {
    @Autowired
    private TaskService taskService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        cq.select(BusSaleman.class, "*");
        cq.addSelect(SysOrganization.class, "organizationName");
        cq.addSelect(SysCompany.class, "companyName");
        cq.addSelect(BusDept.class, "deptName", "deptType");
        cq.addSelect(AppPosition.class, "positionName");
        cq.like(BusSaleman.class, "saleManName");
        cq.eq(BusSaleman.class, "organizationId");
        cq.eq(BusDept.class, "deptType");
        cq.eq(BusSaleman.class, "deptId");
        cq.like(BusSaleman.class, "linkTel");
        cq.eq(true, "saleman.saleManType", 1);
        cq.eq(true, "dept.deptType", DeptTypeEnum.ZY.getCode());
        cq.like(BusSaleman.class, "saleManBusinessNo");
        cq.createJoin(SysCompany.class);
        cq.createJoin(SysOrganization.class);
        cq.createJoin(BusDept.class);
        cq.createJoin(AppPosition.class);
        cq.orderByAsc("saleman.saleManCode");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusSaleman> cq, List<EntityMap> result, EntityMap extra) {
        if (FlymeUtils.isNotEmpty(result)) {
            for (EntityMap entityMap : result) {
                String processInstanceId = entityMap.get("processInstanceId");
                if (FlymeUtils.isNotEmpty(processInstanceId)) {
                    //查询当前审批节点
                    Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                    if (FlymeUtils.isNotEmpty(task)) {
                        entityMap.put("taskName", task.getName());
                        entityMap.put("taskId", task.getId());
                        entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                    }
                }
            }
        }
        return result;
    }

}
